package plan;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Map;

import util.MyCalendar;
import util.MySqlMap;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class PlanAction extends ActionSupport {
	private Plan plan;
	private ArrayList<Plan> planlist;
	private ArrayList<Integer> calendar;
	private int currentYear;
	private int currentMonth;
	private int currentDate;
	private int startofmonth;
	private int endofmonth;
	private int num;
	
	public String write() throws Exception {
		SqlMapClient sqlMap = MySqlMap.getSqlMapInstance();
		new PlanDAO().insert(sqlMap, plan);
		return SUCCESS;
	}
	public String recent() throws Exception {
		SqlMapClient sqlMap = MySqlMap.getSqlMapInstance();
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session = context.getSession();
		planlist = new PlanDAO().recent(sqlMap, (String) session.get("loginid"));
		return SUCCESS;
	}
	
	public String monthly() throws Exception {
		SqlMapClient sqlMap = MySqlMap.getSqlMapInstance();
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session = context.getSession();
		
		if( currentYear == 0 || currentMonth == 0 ){
			Calendar today = Calendar.getInstance();
			currentYear = today.get(Calendar.YEAR);
			currentMonth = today.get(Calendar.MONTH)+1;
		}
		calendar = new MyCalendar(currentYear, currentMonth).getMyCalendar();
		
		startofmonth = calendar.indexOf(1);
		endofmonth = calendar.lastIndexOf(1)-1;
		
		if( startofmonth == endofmonth + 1){
			endofmonth = calendar.size();
		}
		
		Plan monthly = new Plan();
		monthly.setId((String)session.get("loginid"));
		String cYear = String.valueOf(currentYear);
		
		String cMonth = String.valueOf(currentMonth);
		if ( currentMonth < 10)
			cMonth = "0" + cMonth;
		monthly.setStartdate(cYear+cMonth);
		planlist = new PlanDAO().monthly(sqlMap, monthly);
		
		return SUCCESS;
	}
	public String daily() throws Exception {
		SqlMapClient sqlMap = MySqlMap.getSqlMapInstance();
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session = context.getSession();
		
		if( currentYear == 0 || currentMonth == 0  || currentDate == 0){
			Calendar today = Calendar.getInstance();
			currentYear = today.get(Calendar.YEAR);
			currentMonth = today.get(Calendar.MONTH)+1;
			currentDate = today.get(Calendar.DATE);
		}
		calendar = new MyCalendar(currentYear, currentMonth).getMyCalendar();
		
		startofmonth = calendar.indexOf(1);
		endofmonth = calendar.lastIndexOf(1)-1;
		
		Plan daily = new Plan();
		daily.setId((String)session.get("loginid"));
		String cYear = String.valueOf(currentYear);
		String cMonth = String.valueOf(currentMonth);
		if ( currentMonth < 10)
			cMonth = "0" + cMonth;
		String cDate = String.valueOf(currentDate);
		if ( currentDate < 10)
			cDate = "0" + cDate;
		daily.setStartdate(cYear+cMonth+cDate);
		planlist = new PlanDAO().daily(sqlMap, daily);
				
		return SUCCESS;
	}
	public String select() throws Exception {
		SqlMapClient sqlMap = MySqlMap.getSqlMapInstance();
		plan = new PlanDAO().select(sqlMap, num);
		return SUCCESS;
	}
	
	public String update() throws Exception {
		SqlMapClient sqlMap = MySqlMap.getSqlMapInstance();
		num = plan.getNum();
		new PlanDAO().update(sqlMap, plan);
		return SUCCESS;
	}
	
	public String delete() throws Exception {
		SqlMapClient sqlMap = MySqlMap.getSqlMapInstance();
		new PlanDAO().delete(sqlMap, num);
		return SUCCESS;
	}
		
	public Plan getPlan() {
		return plan;
	}

	public void setPlan(Plan plan) {
		this.plan = plan;
	}
	public ArrayList<Plan> getPlanlist() {
		return planlist;
	}
	public void setPlanlist(ArrayList<Plan> planlist) {
		this.planlist = planlist;
	}

	public ArrayList<Integer> getCalendar() {
		return calendar;
	}

	public void setCalendar(ArrayList<Integer> calendar) {
		this.calendar = calendar;
	}
	public int getCurrentYear() {
		return currentYear;
	}
	public void setCurrentYear(int currentYear) {
		this.currentYear = currentYear;
	}
	public int getCurrentMonth() {
		return currentMonth;
	}
	public void setCurrentMonth(int currentMonth) {
		this.currentMonth = currentMonth;
	}
	public int getCurrentDate() {
		return currentDate;
	}
	public void setCurrentDate(int currentDate) {
		this.currentDate = currentDate;
	}
	public int getStartofmonth() {
		return startofmonth;
	}
	public void setStartofmonth(int startofmonth) {
		this.startofmonth = startofmonth;
	}
	public int getEndofmonth() {
		return endofmonth;
	}
	public void setEndofmonth(int endofmonth) {
		this.endofmonth = endofmonth;
	}
	public int getNum() {
		return num;
	}
	public void setNum(int num) {
		this.num = num;
	}
	
}
